//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Tathata.Common;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;
using Tathata.Common.BLL.Managers;
using Tathata.Common.Localization;
using Tathata.Common.Utils;

namespace Tathata.Web.Modules.Products
{
    public partial class ProductAttributesControl : BaseNopUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CreateAttributeControls();
        }

        protected void CreateAttributeControls()
        {
            ProductVariantAttributeCollection productVariantAttributes = ProductVariantAttributeManager.GetAllByProductVariantID(this.ProductVariantID);
            if (productVariantAttributes.Count > 0)
            {
                this.Visible = true;
                foreach (ProductVariantAttribute attribute in productVariantAttributes)
                {
                    Panel divAttribute = new Panel();
                    Label attributeTitle = new Label();
                    if (attribute.IsRequired)
                        attributeTitle.Text = "<span>*</span> ";
                    attributeTitle.Text += attribute.ProductAttribute.Name;
                    attributeTitle.Style.Add("font-weight", "bold");
                    if (!string.IsNullOrEmpty(attribute.ProductAttribute.Description))
                        attributeTitle.Text += string.Format("<div>{0}</div>", attribute.ProductAttribute.Description);

                    attributeTitle.Text += "<br />";
                    divAttribute.Controls.Add(attributeTitle);

                    DropDownList ddlAttributes = new DropDownList();
                    ddlAttributes.ID = attribute.ProductAttribute.Name;
                    ddlAttributes.Items.Add(new ListItem(string.Format(GetLocaleResourceString("ProductVariantsInGridControlControl.SelectAttribute"), attribute.ProductAttribute.Name), "0"));
                    ProductVariantAttributeValueCollection pvaValues = attribute.ProductVariantAttributeValues;
                    foreach (ProductVariantAttributeValue pvaValue in pvaValues)
                    {
                        string pvaValueName = pvaValue.Name;
                        decimal priceAdjustment = pvaValue.PriceAdjustment;
                        decimal priceAdjustmentConverted = CommonHelper.IIF(CurrencyManager.PrimaryStoreCurrency.CurrencyID == NopContext.Current.WorkingCurrency.CurrencyID, priceAdjustment, Decimal.Round(CurrencyManager.ConvertCurrency(priceAdjustment, CurrencyManager.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency), 2, MidpointRounding.AwayFromZero));
                        if (priceAdjustment > 0)
                            pvaValueName += string.Format(" [+{0}]", LocalizationManager.GetCurrencyStringWithoutExchangeRate(priceAdjustmentConverted, false, NopContext.Current.WorkingCurrency));
                        ListItem pvaValueItem = new ListItem(pvaValueName, pvaValue.ProductVariantAttributeValueID.ToString());
                        ddlAttributes.Items.Add(pvaValueItem);
                    }
                    divAttribute.Controls.Add(ddlAttributes);
                    phAttributes.Controls.Add(divAttribute);
                }
            }
            else
                this.Visible = false;
        }

        public int ProductVariantID
        {
            get
            {
                if (ViewState["ProductVariantID"] == null)
                    return 0;
                else
                    return (int)ViewState["ProductVariantID"];
            }
            set { ViewState["ProductVariantID"] = value; }
        }

        public List<int> SelectedAttributeIDs
        {
            get
            {
                List<int> selectedAttributeIDs = new List<int>();
                ProductVariantAttributeCollection productVariantAttributes = ProductVariantAttributeManager.GetAllByProductVariantID(this.ProductVariantID);
                foreach (ProductVariantAttribute attribute in productVariantAttributes)
                {
                    Control ddlAttributes = phAttributes.FindControl(attribute.ProductAttribute.Name);
                    if (ddlAttributes is DropDownList)
                    {
                        int selectedAttributeID = int.Parse(((DropDownList)ddlAttributes).SelectedValue);
                        if (selectedAttributeID > 0)
                            selectedAttributeIDs.Add(selectedAttributeID);
                    }
                }
                return selectedAttributeIDs;
            }
        }
    }
}